﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BuXiaChu.OrderDishes.BLL.SiteConfig;
using BuXiaChu.OrderDishes.BLL.FileStore;
using BuXiaChu.OrderDishes.Model;
using BuXiaChu.OrderDishes.BLL.Utility;

namespace BuXiaChu.OrderDishes.Web.Admin
{
    public partial class Upload : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //FileServer();
            if (IsPostBack)
            {
                foreach (var file in Request.Files)
                {
                    var postFile = Request.Files[file.ToString()];
                    var content = new byte[postFile.ContentLength];
                    postFile.InputStream.Read(content, 0, postFile.ContentLength);

                    UploadFileEntity entity = new UploadFileEntity(111, postFile.FileName, content);
                    IFileUploadProvider upload = new ImageUploadProvider(entity, true);
                    upload.Save();
                    
                }
            }
        }

        private void FileServer()
        {
            #region
            FileServerCollection collection = new FileServerCollection();
            for (var i = 0; i < 3; i++)
            {
                FileServer server = new FileServer();
                server.MapKey = i.ToString();
                server.RootPath = @"\\10.4.5." + i.ToString();
                server.PublishUrl = "http://www.cctv.com";
                server.Indentity = new IdentityModel();
                server.Indentity.UserName = "aaaa";
                server.Indentity.Impersonate = true;
                server.Indentity.Password = "";
                server.Indentity.Domain = "";

                server.MimeItemList = new MimeItemCollection();
                MimeItem image = new MimeItem();
                image.Key = "image";
                image.MimeTypeList = ".jpg;.bmp;.gif";
                server.MimeItemList.Add(image);
                var attachment = new MimeItem();
                attachment.Key = "attachment";
                attachment.MimeTypeList = ".rar";
                server.MimeItemList.Add(attachment);

                collection.Add(server);
            }
            var xmlText = SerializerUtility.SerializeToXmlString(collection);
            var tmpl = SerializerUtility.DeserializeFromXmlString(xmlText, typeof(FileStoreServerCollection));
            Response.Write(xmlText);
            Response.End();
            #endregion
        }

        private void Test()
        {
            #region

            //-------------------
            //List<FileStoreServerMap> servermap = new List<FileStoreServerMap>();
            //FileStoreServerMap map = new FileStoreServerMap();
            //map.MapName = "product01";
            //map.MimeItemKey = "image";
            //servermap.Add(map);

            //FileStoreServerMap attechment = new FileStoreServerMap();
            //attechment.MapName = "attechment";
            //attechment.MimeItemKey = "attechment";
            //servermap.Add(attechment);
            //Response.Write(SerializerUtility.SerializeToString(servermap));

            //--------------

            Response.Write(SiteConfigManager.Intance.FileServerMapList[0].MapName);
            //ConfigFilePathSetting setting = new ConfigFilePathSetting();
            //var filePath = IOUtility.GetRootedFilePath(@"Config\ConfigFilePathSetting.config");
            //var setting = (ConfigFilePathSetting)SerializerUtility.DeserializeFromFile(filePath, typeof(ConfigFilePathSetting));
            //Response.Write(SerializerUtility.SerializeToString(setting));
            Response.End();
            #endregion
        }
    }
}
